package com.example.lenovo.mywechat;

import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity implements OnClickListener{
    private ImageButton mBtn_home;
    private ImageButton mBtn_contact;
    private ImageButton mBtn_find;
    private ImageButton mBtn_me;
    private HomeFragment homeFragment=null;
    private ContactFragment contactFragment=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_new);

        mBtn_home=findViewById(R.id.btn_home);
        mBtn_contact=findViewById(R.id.btn_contact);
        mBtn_find=findViewById(R.id.btn_find);
        mBtn_me=findViewById(R.id.btn_me);

        mBtn_home.setOnClickListener(this);
        mBtn_contact.setOnClickListener(this);
        mBtn_find.setOnClickListener(this);
        mBtn_me.setOnClickListener(this);

        selectTab(0);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.btn_home:
                selectTab(0);
                break;
            case R.id.btn_contact:
                selectTab(1);
                break;
            case R.id.btn_find:
                selectTab(2);
                break;
            case R.id.btn_me:
                selectTab(3);
                break;
             default:
                 Log.e("MainActivity","on click error!");
        }
    }

    private void selectTab(int i) {
        setTabButtonDefault();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                mBtn_home.setImageResource(R.mipmap.home_selected);
                if(homeFragment==null){
                    homeFragment=new HomeFragment();
                    transaction.add(R.id.MA_framelayout,homeFragment);
                }else{
                    transaction.show(homeFragment);
                }
                break;
            case 1:
                mBtn_contact.setImageResource(R.mipmap.category_selected);
                if (contactFragment==null){
                    contactFragment=new ContactFragment();
                    transaction.add(R.id.MA_framelayout,contactFragment);
                }else{
                    transaction.show(contactFragment);
                }
                break;
            case 2:
                mBtn_find.setImageResource(R.mipmap.find_selected);
                Intent intent = new Intent(MainActivity.this,ChattingRoomActivity.class);
                startActivity(intent);
                break;
            case 3:
                mBtn_me.setImageResource(R.mipmap.mine_selected);
                break;
             default:
                 break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if(homeFragment!=null){
            transaction.hide(homeFragment);
        }
        if (contactFragment!=null){
            transaction.hide(contactFragment);
        }
    }

    private void setTabButtonDefault() {
        mBtn_home.setImageResource(R.mipmap.home_normal);
        mBtn_contact.setImageResource(R.mipmap.category_normal);
        mBtn_find.setImageResource(R.mipmap.find_normal);
        mBtn_me.setImageResource(R.mipmap.mine_normal);
    }
}
